package com.itboye.jinpuyiyuan.activity.home.hospitalserver;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.activity.me.HospitalActivity;
import com.itboye.jinpuyiyuan.activity.me.PaymentRecordActivity;
import com.itboye.jinpuyiyuan.activity.pay.PayActivity;
import com.itboye.jinpuyiyuan.adapter.InHospitalAdapter;
import com.itboye.jinpuyiyuan.adapter.TestServerOrderAdapter;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.DayOrderBean;
import com.itboye.jinpuyiyuan.bean.HospitalInfoBean;
import com.itboye.jinpuyiyuan.bean.TestServerOrder;
import com.itboye.jinpuyiyuan.bean.YJJBean;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2017/12/15 0015.
 * 住院服务
 */

public class HospitalServerActivity extends BaseActivity implements Observer, InHospitalAdapter.OnItemClickListenner {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;

    private Button prepayTv,dayListTv;
    private TextView totalTv;
    private LinearLayout prepayLl; //预交金支付
    private LinearLayout dayOrderInfo; //日清单
    private LinearLayout bottomLl; //底部确定的ll
    private BranchAddressPresenter presenter;

    private String uid;

    //预交金
    private TextView name;         //姓名
    private TextView identityNum; //身份证号
    private TextView hospitalNum;//住院流水号

    private TextView payRecordTv; //缴费记录

    //日清单
    TextView inHospitalTime;// 入院时间
    TextView patientName; //姓名
    TextView natureTv; //性质
    TextView departmentTv; //科室
    TextView bedNum; //床号
    TextView hospitalNumber; //住院号码
    TextView fee; //费用总额
    TextView prepaySum; //预交金总额
    TextView payTv;//需支付金额
    TextView firstPay,twoPay,threePay,fourPay;
    EditText editPay;

    String payAmount="250000";

    TextView inHospitalTv; //已住院5天
    RecyclerView hospitalRv; //住院记录
    InHospitalAdapter adapter; //住院记录adapter
    List<DayOrderBean.InList> lists;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_hospitalserver);
        leftTv.setVisibility(View.GONE);
        rightImg.setVisibility(View.GONE);
        tvTitle.setText("住院服务");
        bottomLl.setVisibility(View.VISIBLE);
        uid= SPUtils.get(this,null, Const.UID,"")+"";
        lists=new ArrayList<>();
        presenter=new BranchAddressPresenter(this);

        showProgressDialog("数据加载中,请稍后...",false);
        presenter.HospitalInfo(uid);
        presenter.dailyList(uid);

        adapter=new InHospitalAdapter(this,lists,this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        hospitalRv.setLayoutManager(linearLayoutManager);
        hospitalRv.setAdapter(adapter);

        editPay.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                 if (s.length()>0){
                     payAmount=Integer.parseInt(s.toString().trim())*100+"";
                     payTv.setText("需支付: "+Integer.parseInt(s.toString().trim())+"元");
                 }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });



    }
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;

            case R.id.prepayTv:
                cancleTableState();
                prepayLl.setVisibility(View.VISIBLE);
                bottomLl.setVisibility(View.VISIBLE);
                prepayTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_bluesolid_btn));
                prepayTv.setTextColor(getResources().getColor(R.color.white));
                break;

            case R.id.dayListTv:
                cancleTableState();
                dayOrderInfo.setVisibility(View.VISIBLE);
                bottomLl.setVisibility(View.GONE);
                dayListTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_bluesolid_btn));
                dayListTv.setTextColor(getResources().getColor(R.color.white));

                break;
            case R.id.totalTv:
                presenter.getYJJPay(uid,payAmount);
//                Toast.makeText(this,"待开发",Toast.LENGTH_SHORT).show();
                break;
            case R.id.payRecordTv:
                startActivity(new Intent(this, HospitalActivity.class));
                break;

            case R.id.firstPay:

                editPay.setText(1000+"");

                break;
            case R.id.twoPay:

                editPay.setText(3000+"");

                break;
            case R.id.threePay:

                editPay.setText(5000+"");

                break;
            case R.id.fourPay:

                editPay.setText(7000+"");

                break;
            case R.id.editPay:

                break;
//            PaymentRecordActivity
        }
    }

    public void cancleTableState(){
         dayOrderInfo.setVisibility(View.GONE);
         prepayLl.setVisibility(View.GONE);
         prepayTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_blueedge));
         prepayTv.setTextColor(getResources().getColor(R.color.title_blue));
         dayListTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_blueedge));
         dayListTv.setTextColor(getResources().getColor(R.color.title_blue));
    }

    @Override
    public void update(Observable o, Object arg) {

        ResultEntity entity = handlerError(arg);
        if (entity!=null){
            try{
                closeProgressDialog();
            }catch (Exception e){

            }
            if (entity.getEventType()==BranchAddressPresenter.By_Hospitalization_advanceGold_success){

                HospitalInfoBean bean= (HospitalInfoBean) entity.getData();
                name.setText("姓名:  "+bean.getName());
                identityNum.setText("证件号码:  "+bean.getId_no());
                hospitalNum.setText("住院流水号:  "+bean.getPatient_no());


            }else if (entity.getEventType()==BranchAddressPresenter.By_Hospitalization_advanceGold_fail){
                MAlert.alert(entity.getMsg());

            }else if (entity.getEventType()==BranchAddressPresenter.By_Hospitalization_dailyList_success){
                DayOrderBean bean= (DayOrderBean) entity.getData();
                inHospitalTime.setText(bean.getIn_date());
                patientName.setText(bean.getName());
                natureTv.setText(bean.getPatientnature());
                departmentTv.setText(bean.getDeptname());
                bedNum.setText(bean.getBednumber());
                hospitalNumber.setText(bean.getPatientno());
                fee.setText(bean.getFee());
                prepaySum.setText(bean.getPrepay());

                if (bean.getIn_list().size()>0){
                    inHospitalTv.setVisibility(View.VISIBLE);
                    inHospitalTv.setText("已住院"+bean.getIn_list().size()+"天");
                }else {
                    inHospitalTv.setVisibility(View.GONE);
                }

                adapter.addData(bean.getIn_list());
            }else if (entity.getEventType()==BranchAddressPresenter.By_Hospitalization_dailyList_fail){
                MAlert.alert(entity.getMsg());

            }else if (entity.getEventType()==BranchAddressPresenter.By_Hospitalization_inHosPrePay_success){
                YJJBean bean= (YJJBean) entity.getData();
                Intent intent = new Intent(this, PayActivity.class);
                intent.putExtra("order_id",bean.getOrder_code());
                intent.putExtra("remark",bean.getNote());
                startActivity(intent);
            }else if (entity.getEventType()==BranchAddressPresenter.By_Hospitalization_inHosPrePay_fail){
                MAlert.alert(entity.getMsg());
            }

        }
    }

    //money 费用总额
    @Override
    public void onRvItemClick(String money,String date) {
        Intent intent = new Intent(this, DailyListDetailActivity.class);
        intent.putExtra("money",money);
        intent.putExtra("date",date);
        startActivity(intent);

    }

//    class MyAdapter extends BaseAdapter{
//
//        private ArrayList<String> list;
//        private Context context;
//        private LayoutInflater inflater;
//        public  MyAdapter(Context context,ArrayList<String> list){
//
//            this.list=list;
//            this.context=context;
//            inflater=LayoutInflater.from(context);
//        }
//        @Override
//        public int getCount() {
//            return list.size();
//        }
//
//        @Override
//        public Object getItem(int position) {
//            return list.get(position);
//        }
//
//        @Override
//        public long getItemId(int position) {
//            return position;
//        }
//
//        @Override
//        public View getView(int position, View convertView, ViewGroup parent) {
//            MyViewHolder myViewHolder;
//            if (convertView==null){
//                myViewHolder=new MyViewHolder();
//                convertView=inflater.inflate(R.layout.item_hospitalserver_gridelist,parent,false);
//                myViewHolder.tableItemTv=convertView.findViewById(R.id.tableItemTv);
//                convertView.setTag(myViewHolder);
//            }else {
//                myViewHolder= (MyViewHolder) convertView.getTag();
//            }
//            myViewHolder.tableItemTv.setText(list.get(position));
//
//
//            return convertView;
//        }
//
//        class MyViewHolder{
//            TextView tableItemTv;
//        }
//    }
}
